<?php
	require_once("database.php");
	
	class wall_handler extends database{
		public $result;
		public $result_wallid;
		public $result_usrposted;
		
		function __construct(){
			parent::__construct();
		}

                function  __destruct() {

                }

		function post_wall($usr_id, $usr_tujuan, $wall_body){
			$query = "INSERT INTO wall_post (usr_posted,usr_tujuan,wall_body) VALUES('$usr_id','$usr_tujuan','$wall_body')";
			
			$this->connect();
			mysql_query($query,$this->conn);
			mysql_close();
		}
	
		
		function add_comment($w_id, $usr, $body){
			$q_order = "SELECT max(order_wall_com) AS maks FROM wall_comment WHERE wall_id=$w_id";
			$this->connect();
			$max_order = mysql_result(mysql_query($q_order), "maks");
			$m = $max_order;
			$m++;
			mysql_close();

			$query = "INSERT INTO wall_comment(wall_id,usr_commented,body,order_wall_com) VALUES($w_id,'$usr','$body', $m)";
			$this->connect();
			mysql_query($query,$this->conn);
			mysql_close();
		}

		function add_tagged($w_id, $usr_id){
			$query = "INSERT INTO tagged_in_wall VALUES ('$w_id','$usr_id')";
			$this->connect();
			mysql_query($query,$this->conn);
			mysql_close();
		}
		
		
		function retr_tagged_user($w_id, $col){
			
			$query = "Select usr_tagged from tagged_in_wall where tagged_in_wall.wall_id = $w_id";
			$res = $this->exec_q($query,$col);
                        //hasil = $this->result[idx]
			return $res;
		}
		
		function retr_privacy($usr_id){
			
                    $query = "Select wall_privacy FROM user WHERE usr_email='$usr_id'";

                    $this->connect();
                    mysql_query($query,$this->conn);
                    mysql_close();
		}
		
		function retr_comment($w_id, $col){
			$query = "SELECT user.usr_email, hasil1.body FROM (SELECT * FROM wall_comment WHERE wall_id = $w_id ORDER BY
                                    order_wall_com) AS hasil1, user WHERE hasil1.usr_commented = user.usr_email";
			
			$res = $this->exec_q($query,$col);
			return $res;
		}
		
		
		function retr_post_byUsr($usr_id){
				$this->connect();
				$query = "Select * from wall_post where usr_tujuan = '$usr_id' ORDER BY wall_id DESC";
				
				$exec = mysql_query($query,$this->conn);
				$iter=0;
				
			
				while($iter < mysql_num_rows($exec)){
					//result yg bagian ini jadi array of array
					//view wall body
					$e[$iter] = mysql_result($exec, $iter, "wall_body");
                                        
					//store wall id
					$result[$iter] = mysql_result($exec, $iter, "wall_id");
					
					//store user posted
					$result_usrposted[$iter] = mysql_result($exec, $iter, "usr_posted");
					
					$res_tag = $this->retr_tagged_user($result[$iter],"usr_tagged");
					
					//view tagged user
                                        $e[$iter] .= "/";
					if ($res_tag != NULL){
						for ($i=0; $i<count($res_tag); $i++){
							//echo "^ ". $r. " <br/>";
                                                    $e[$iter] .= $res_tag[$i]. ";";
						}
						
					}
                                        else{
                                             $e[$iter] .= "". ";";
                                        }
                                        $e[$iter] .= "&";
					
					//view comments
					$res_b = $this->retr_comment($result[$iter],"body");
					//ganti
					$res_n = $this->retr_comment($result[$iter],"usr_email");
					
					if ($res_n != NULL){
						for ($i=0; $i<count($res_n); $i++){
							$e[$iter] .= $res_n[$i]. ";";
                                                        $res_n[$i];
						}
                                        }
                                        else{
                                            $e[$iter] .= "". ";";
                                        }
                                        $e[$iter] .= "#";
                                        if ($res_b != NULL){
                                                for ($i=0; $i<count($res_b); $i++){
							$e[$iter] .= $res_b[$i]. ";";
						}
							
					}
                                        else{
                                            $e[$iter] .= "". ";";
                                        }

					$iter++;
				}	
				$this->result = $e;
				// tambahan
				$this->result_wallid=$result;
				$this->result_usrposted = $result_usrposted;
                                //hasil = $this->result[idx][idx2]
                                /*
                                 * $this->result[0] = wall body
                                 * $this->result[1] = tagged user
                                 * $this->result[2] = username of commentator
                                 * $this->result[3] = comment body
                                 */
				mysql_close();                    
		}
                
                function parse_wall($wall){
                    $wbody = explode('/', $wall);
                    $wtagged = explode('&', $wbody[1]);
                    $wcom = explode('#', $wtagged[1]);

                    //echo "<br/>".$wbody[0]. "~". $wbody[1]. "<br/>";
                    //echo $wtagged[0]. "~". $wtagged[1]. "<br/>";
                    //echo $wcom[0]. "~". $wcom[1]. "<br/>";

                    $parsed[0] = $wbody[0]; //wall bady
                    $parsed[1] = $wtagged[0]; //wall tagged
                    $parsed[2] = $wcom[0]; //wall commentator
                    $parsed[3] = $wcom[1]; //wall_commbody

                    return $parsed;
                }

                function parse_walltagged($par_w){
                    return explode(';', $par_w[1]);
                }

                 function parse_wallcommentator($par_w){
                    return explode(';', $par_w[2]);
                }

                 function parse_wallcommbody($par_w){
                    return explode(';', $par_w[3]);
                }
                

                 function del_wallpost($w_id){
                       $query = "DELETE FROM wall_post WHERE wall_id='$w_id'";
                       $this->connect();
                       mysql_query($query,$this->conn);
                       mysql_close();
                 }

                 function del_wall_comment($w_id, $usr_com, $body){
                       $query = "DELETE FROM wall_comment WHERE wall_id='$w_id' AND usr_commented='$usr_com' AND body='$body'";
                       $this->connect();
                       mysql_query($query,$this->conn);
                       mysql_close();
                 }
	}
	
	
	
	/*if (isset($_POST['submit_wall'])){
		post_wall($_POST['usr_id'],$_POST['body']);
		
	}*/
?>
